package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.mapper.DtsPermissionMapper;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsPermission;
import com.qf.pojo.DtsRegion;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.rmi.Remote;
import java.security.Permissions;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author 金园
 * @create 2022/5/17
 * @Description
 * @className DtsRoleServiceImpl
 * @packageName com.qf.service.impl
 */
@Service
public class DtsRoleServiceImpl implements DtsRoleService {
    @Autowired
    DtsRoleMapper roleMapper;

    @Autowired
    DtsAdminMapper adminMapper;

    @Autowired
    DtsPermissionMapper permissionMapper;

    @Override
    public Set<String> findRoleById(Integer[] roleIds) {
        LambdaQueryWrapper<DtsRole> qw = new LambdaQueryWrapper();
        List<DtsRole> roles = roleMapper.selectList(qw);
        //stream:转化成流。map：要映射成什么样的对象。collect：指定返回集合类型
//        Set<String> roleStrs = roles.stream().map(role -> {
//            return role.getName();
//        }).collect(Collectors.toSet());
        return roles.stream().map(role -> {
            return role.getName();
        }).collect(Collectors.toSet());
    }

    @Override
    public List<DtsRole> findAll() {
        return  roleMapper.selectList(null);
    }

    @Override
    public IPage<DtsRole> queryPage(Integer page, Integer limit, String sort, String order, String rolename) {
        //创建分页条件参数
        IPage<DtsRole> iPage = new Page(page,limit);
        //插件条件查询
        QueryWrapper<DtsRole> qw = new QueryWrapper();
        if (!StringUtils.isEmpty(rolename)){
            qw.like("name",rolename);
        }
        if (!StringUtils.isEmpty(sort)&&StringUtils.isEmpty(order)){
            if (order.equalsIgnoreCase("desc")){
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }
        roleMapper.selectPage(iPage,qw);
        return iPage;
    }

    @Override
    public void update(DtsRole role) {
        roleMapper.updateById(role);
    }

    @Override
    public boolean  delete(DtsRole role) {
        //如果该角色被管理员使用，则不能删除
        LambdaQueryWrapper<DtsAdmin> qw1 = new LambdaQueryWrapper<>();
        //指定 只查询  roleIds 列，其他列不查询
        qw1.select(DtsAdmin::getRoleIds);
        List<DtsAdmin> adminList = adminMapper.selectList(qw1);
        for (DtsAdmin admin : adminList) {
            Integer[] roleIds = admin.getRoleIds();
            for (Integer roleId : roleIds) {
                //如果数据查询到的角色id与 要删除的角色id相同，则不能删除
                if(roleId.equals(role.getId())) {
                    return false;
                }
            }
        }

        //如果该角色关联了权限，则不能删除
        LambdaQueryWrapper<DtsPermission> qw2 = new LambdaQueryWrapper<>();
        qw2.eq(DtsPermission::getRoleId, role.getId());
        Integer count = permissionMapper.selectCount(qw2);
        if(count >= 1) {
            return  false;
        }

        //如果没有被管理员和权限关联，则删除
        roleMapper.deleteById(role.getId());
        return true;
    }

    @Override
    public boolean isSuperAdmin(Integer roleId) {

        DtsRole dtsRole = roleMapper.selectById(roleId);
        if ("超级管理员".equalsIgnoreCase(dtsRole.getName())){
            return true;
        }
        return false;
    }


}
